const { finduser } = require('../../service/main/mainlogin')
const jwt = require('jsonwebtoken')
module.exports = {
    async mainlogin(ctx, next) {
        console.log(ctx.header)
        const { username, password } = ctx.query
        if (username.trim().length === 0 || password.trim().length === 0) {
            ctx.body = {
                code: 400,
                msg: '用户名或密码不能为空'
            }
        } else {
            // ctx.body = {
            const rudata = await finduser({ name: username, password })
            if (rudata) {
                const { password, ...alldata } = rudata
                return ctx.body = {
                    code: 200,
                    msg: '登录成功',
                    data: alldata,
                    token: jwt.sign(alldata, 'ximo', { expiresIn: '1d' })
                }
            }
            return ctx.body = {
                code: 0,
                msg: '用户名或密码错误'
            }
        }
    }
}